package com.zjt.takeoutproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjt.takeoutproject.common.CustomException;
import com.zjt.takeoutproject.dto.SetmealDto;
import com.zjt.takeoutproject.entity.Setmeal;
import com.zjt.takeoutproject.mapper.SetmealMapper;
import com.zjt.takeoutproject.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-06-13
 */

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息，操作setmeal的新增
        this.save(setmealDto);

    }

    @Transactional
    @Override
    public void removeSetmealById(List<Long> ids) {
        //查询套餐状态是否可以删除
        List<Setmeal> setmealList = setmealMapper.status_SelectBatchIds(ids);
        //如果不能删除，抛出业务异常
        for (Setmeal setmeal : setmealList) {
            Integer status = setmeal.getStatus();
            //如果不是在售卖,则可以删除
            if (status == 0){
                //如果能删除，删除套餐表中的数据
                this.removeById(setmeal.getId());
            }else {
                //此时应该回滚,因为可能前面的删除了，但是后面的是正在售卖
                throw new CustomException("删除套餐中有正在售卖套餐,无法全部删除");
            }
        }


    }
}
